package view;

import actor.Actor;
import actor.Player;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.text.PlainDocument;
import java.awt.*;

public class ActorView extends JComponent {
    private final Actor actor;
    private final JLabel imageLabel;
    private final JProgressBar healthBar = new JProgressBar();

    public ActorView(Actor actor, boolean isPlayer) {
        this.actor = actor;
        String imageName = actor.getImage();
        String fileName = "src/image/" + imageName + ".png";
        ImageIcon image = new ImageIcon(fileName);
        if (isPlayer) {
            image.setImage(image.getImage().getScaledInstance(200, 274, Image.SCALE_DEFAULT));
        } else {
            image.setImage(image.getImage().getScaledInstance(200, 200, Image.SCALE_DEFAULT));
        }
        imageLabel = new JLabel(image);
        JLabel nameLabel = new JLabel(actor.getName());

        nameLabel.setBounds(100, 200, 100, 20);
        imageLabel.setBounds(0, 0, 200, 200);
        this.add(nameLabel);
        this.add(imageLabel);
        healthBar.setOrientation(SwingConstants.HORIZONTAL);
        healthBar.setBounds(10, 220, 180, 20);
        healthBar.setMinimum(0);
        healthBar.setMaximum(100);
        healthBar.setValue(100);
        this.add(healthBar);
        this.setVisible(true);
        this.setBounds(0, 0, 210, 300);
        Border line = BorderFactory.createLineBorder(Color.BLACK, 2);
        imageLabel.setBorder(line);
    }

    public void beingAttack(int power) {
        int nowHealth = actor.minusHealth(power);
        int nowPercent = (int) (((double) nowHealth / actor.getLife()) * 100);
        healthBar.setValue(nowPercent);
        SwingUtilities.updateComponentTreeUI(this);
    }

    public void beingHeal(int power) {
        Player player = (Player) actor;
        int nowHealth = player.addHealth(power);
        int nowPercent = (int) (((double) nowHealth / actor.getLife()) * 100);
        healthBar.setValue(nowPercent);
        SwingUtilities.updateComponentTreeUI(this);
    }

    public void beingPower() {
        Player player = (Player) actor;
        player.doublePower();
    }


    public void enter() {
        imageLabel.setBorder(new LineBorder(Color.BLUE, 4));
    }

    public void exit() {
        imageLabel.setBorder(new LineBorder(Color.BLACK, 4));
    }

    public void killed() {
        imageLabel.setBorder(new LineBorder(Color.RED, 4));
    }

    public Actor getActor() {
        return actor;
    }
}
